package com.gable.report.service.impl;

import com.gable.report.common.util.MonthEnumUtil;
import com.gable.report.dao.IChartsForHospitalMaintainDao;
import com.gable.report.model.ChartsAssetsMaintainInfoView;
import com.gable.report.model.ChartsForMaintainRecord;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.ChartsMaintainInfoView;
import com.gable.report.service.ChartsForHospitalMaintainInfoService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.gable.report.util.ChartsUtil.getParamMap;
import static com.gable.report.util.ChartsUtil.handleNull;
import static com.gable.report.util.ChartsUtil.listForPage;


/**
 * Created by lvdouStart on 2017/8/16.
 * 医院报表维修情况service实现类
 */
@Service
public class ChartsForHospitalMaintainInfoServiceImpl implements ChartsForHospitalMaintainInfoService {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    IChartsForHospitalMaintainDao chartsForHospitalMaintainDao;

    @Override
    public Map<String, Object> queryAssetsForOverview(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsMaintainInfoView allAssets = chartsForHospitalMaintainDao.queryAllAssets(paramMap);
        paramMap.put("asId",1);
        paramMap.put("asName","'大维修'");
        paramMap.put("maintainSize",1);
        Map<String,Object> bigRepair = chartsForHospitalMaintainDao.queryMaintainSizeInfo(paramMap);
        paramMap.put("asId",2);
        paramMap.put("asName","'中维修'");
        paramMap.put("maintainSize",2);
        Map<String,Object> middleRepair = chartsForHospitalMaintainDao.queryMaintainSizeInfo(paramMap);
        paramMap.put("asId",3);
        paramMap.put("asName","'小维修'");
        paramMap.put("maintainSize",3);
        Map<String,Object> smallRepair = chartsForHospitalMaintainDao.queryMaintainSizeInfo(paramMap);
        returnMap.put("allAssets",allAssets);
        returnMap.put("list", Arrays.asList(bigRepair,middleRepair,smallRepair));
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDepartments(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDepartments(paramMap);
        returnMap.put("count",list.size());
        returnMap.put("list",listForPage(list,chartsHospitalQuery));
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceModels(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDeviceModels(paramMap);
        returnMap.put("count",list.size());
        returnMap.put("list",listForPage(list,chartsHospitalQuery));
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceTypes(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.querySecondDeviceTypes(paramMap);
        returnMap.put("count",list.size());
        returnMap.put("list",listForPage(list,chartsHospitalQuery));
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceBrands(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDeviceBrands(paramMap);
        returnMap.put("count",list.size());
        returnMap.put("list",listForPage(list,chartsHospitalQuery));
        return returnMap;
    }


    @Override
    public Map<String, Object> queryForMonth(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        int curYear = (Integer) paramMap.get("curYear");
        paramMap.put("lastYearEnd",new StringBuffer().append("'").append(curYear).append("-1-1'").toString());
        List<ChartsMaintainInfoView> list = chartsForHospitalMaintainDao.queryForMonth(paramMap);
        for(ChartsMaintainInfoView obj: list){
            obj.setName(MonthEnumUtil.getValue(obj.getId()));
        }
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceInfoByModelName(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsAssetsMaintainInfoView> list = chartsForHospitalMaintainDao.queryDeviceInfoByModelName(paramMap);
        returnMap.put("count",list.size());
        returnMap.put("list", listForPage(handleNull(list),chartsHospitalQuery));
        return returnMap;
    }

    @Override
    public Map<String, Object> queryMaintainRecord(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainRecord> list = chartsForHospitalMaintainDao.queryMaintainRecord(paramMap);
        returnMap.put("count",list.size());
        List<ChartsForMaintainRecord> returnList = new ArrayList<>();
        //分页数据
        int start = chartsHospitalQuery.getPage().getStart();
        int limit = chartsHospitalQuery.getPage().getLimit();
        int max;
        if(start+limit > list.size()){
            max = list.size();
        }else{
            max = start + limit;
        }
        for(int i = start;i<max;i++){
            returnList.add(list.get(i));
        }
        returnMap.put("list", returnList);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHaltRecord(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainRecord> list = chartsForHospitalMaintainDao.queryHaltRecord(paramMap);
        returnMap.put("list", list);
        return returnMap;
    }


}
